<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class GoodsCategory extends Model
{
    use HasDateTimeFormatter;
    use HasFactory;

    const STATUS_NO = 0; //下架
    const STATUS_YES = 1; //上架
    const HOME_NO = 0; //是否首页显示 0否 1是
    const HOME_YES = 1; //是否首页显示 0否 1是

    const LIMIT_PURCHASE_NO = 0;   //不限购
    const LIMIT_PURCHASE_YES = 1;  //限购

    const LIMIT_PURCHASE = [
        self::LIMIT_PURCHASE_NO => '否',
        self::LIMIT_PURCHASE_YES => '是',
    ];

    protected $fillable = [
        'title',
        'pid',
        'pic',
        'sort',
        'status',
        'updated_at',
        'limit_purchase',
        'country'
    ];

    /**
     * 获取分类
     * @param $select array 返回字段
     * @param $status int 上下架
     * @param $orderBy array 排序字段 ['sort', 'asc'] 第一个参数排序字段 第二个参数排序方式
     * * @param bool $is_home
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getCategory(array $select, int $status, array $orderBy, bool $is_home=false)
    {
        $model = self::query()->where('country','=',config('app.goods_country'));
        if(!empty($status)) {
            $model->where('status', $status);
        }
        if($is_home) {
            $model->where('is_home', self::HOME_YES);
        }
        if(!empty($orderBy)) {
            $model->orderBy($orderBy[0], $orderBy[1]);
        }
        if(!empty($select)) {
            $model->select($select);
        }
        return $model->get();
    }

}
